<template>
  <div class="u-page u-page-color-other">
    <section class="aui-flexView">
      <section class="aui-scrollView">
        <div class="aui-tab" data-ydui-tab="">
          <ul class="tab-nav">
            <li :class="current == 0?'tab-nav-item tab-active':'tab-nav-item'" @click="change(0)">
              未使用({{NotUsedNum?NotUsedNum:0}})
            </li>
            <li :class="current == 1?'tab-nav-item tab-active':'tab-nav-item'" @click="change(1)">
              已使用({{UsedNum?UsedNum:0}})
            </li>
            <li :class="current == 2?'tab-nav-item tab-active':'tab-nav-item'" @click="change(2)">
              已过期({{ExpiredNum?ExpiredNum:0}})
            </li>
          </ul>
          <div class="tab-panel">
            <div :class="current == 0?'tab-panel-item tab-active':'tab-panel-item'" v-if="current == 0">
              <div class="tab-item" v-if="couponList0 && couponList0.length>0">
                <div class="tab-item-list-pink" v-for="(item,index) in couponList0" :key="index">
                  <div class="aui-flex">
                    <div class="aui-left-change">
                      <h2 class="font-baseline">
                        <em>￥</em>
                        <span>{{formatAmount(item.couponAmount)}}</span>
                      </h2>
					  <p>无门槛</p>
                      <div class="aui-icon-circle"></div>
                    </div>
                    <div class="aui-flex-box aui-flex-box-two">
                      <h3>
                        <span class="u-fz-14">{{item.useScope=='ALL'?'玫丽生活购物通用券':'玫丽生活购物单品券'}}</span>
                      </h3>
                      <div class="aui-flex aui-flex-bor-bom">
                        <div class="aui-flex-box aui-flex-box-flow">有效期至{{item.validateTime.split(' ')[0]}}</div>
                        <div class="aui-btn-use" @click="toUseCoupon(item)">立即使用</div>
                      </div>
                      <div class="aui-flex aui-flex-show">
						  {{formatDesc(item)}}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
			  <div v-else>
			  	<div class="u-text-center" style="padding-top: 20%;">
			  		<u-empty text="暂无数据" mode="data"></u-empty>
			  	</div>
			  </div>
            </div>
            <div :class="current == 1?'tab-panel-item tab-active':'tab-panel-item'" v-if="current == 1">
              <div class="tab-item" v-if="couponList1 && couponList1.length>0">
                <div class="tab-item-list tab-item-list-default" v-for="(item,index) in couponList1" :key="index">
                  <div class="aui-flex">
                    <div class="aui-left-change">
						<h2 class="font-baseline">
						  <em>￥</em>
						  <span>{{formatAmount(item.couponAmount)}}</span>
						</h2>
						<p>无门槛</p>
						<div class="aui-icon-circle"></div>
                    </div>
                    <div class="aui-flex-box aui-flex-box-two">
						<span class="aui-icon-used">
							<img src="https://dev-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/icon/Common/icon-used.png" alt="">
						</span>
						<h3>
						  <span class="u-fz-14">{{item.useScope=='ALL'?'玫丽生活购物通用券':'玫丽生活购物单品券'}}</span>
						</h3>
                      <div class="aui-flex aui-flex-bor-bom u-pb-5">
                        <div class="aui-flex-box aui-flex-box-flow">有效期至{{item.validateTime.split(' ')[0]}}</div>
                      </div>
                      <div class="aui-flex aui-flex-show">
					 	 {{formatDesc(item)}}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
			  <div v-else>
			  	<div class="u-text-center" style="padding-top: 20%;">
			  		<u-empty text="暂无数据" mode="data"></u-empty>
			  	</div>
			  </div>
            </div>
            <div :class="current == 2?'tab-panel-item tab-active':'tab-panel-item'" v-if="current == 2">
              <div class="tab-item" v-if="couponList2 && couponList2.length>0">
                <div class="tab-item-list tab-item-list-default" v-for="(item,index) in couponList2" :key="index">
                  <div class="aui-flex">
                    <div class="aui-left-change">
						<h2 class="font-baseline">
						  <em>￥</em>
						  <span>{{item.couponAmount?item.couponAmount:0}}</span>
						</h2>
						<p>无门槛</p>
						<div class="aui-icon-circle"></div>
                    </div>
                    <div class="aui-flex-box aui-flex-box-two">
                      <h3>
                        <span class="u-fz-14">{{item.useScope=='ALL'?'玫丽生活购物通用券':'玫丽生活购物单品券'}}</span>
                      </h3>
                      <div class="aui-flex aui-flex-bor-bom u-pb-5">
                        <div class="aui-flex-box aui-flex-box-flow">{{item.validateTime.split(' ')[0]}}</div>
                      </div>
                      <div class="aui-flex aui-flex-show">
                           {{formatDesc(item)}}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
			  <div v-else>
			  	<div class="u-text-center" style="padding-top: 20%;">
			  		<u-empty text="暂无数据" mode="data"></u-empty>
			  	</div>
			  </div>
            </div>
          </div>
        </div>
      </section>
    </section>
  </div>
</template>

<script>
export default {
  data() {
    return {
      current: 0,
      pageNum0: 1,
      pageSize0: 10,
      couponList0: [],
	  pageNum1: 1,
	  pageSize1: 10,
	  couponList1: [],
	  pageNum2: 1,
	  pageSize2: 10,
	  couponList2: [],
      userDetail: {},
	  NotUsedNum: 0,
	  UsedNum: 0,
	  ExpiredNum: 0,
    }
  },
  onPullDownRefresh() {
	  if(this.current == 0){
		  this.pageNum0 = 1
		  this.getUserCoupons()
	  }else if(this.current == 1){
		  this.pageNum1 = 1
		  this.getUserCoupons()
	  }else if(this.current == 2){
		  this.pageNum2 = 1
		  this.getUserCoupons()
	  }
  },
  onReachBottom() {
	  if(this.current == 0){
		  this.pageNum0 += 1
		  this.getUserCoupons()
	  }else if(this.current == 1){
		  this.pageNum1 += 1
		  this.getUserCoupons()
	  }else if(this.current == 2){
		  this.pageNum2 += 1
		  this.getUserCoupons()
	  }
  },
  onLoad(options) {
    this.userDetail = this.$store.state.userInfo;
    this.getUserCoupons();
    this.getUserCouponsCount();
  },
  methods: {
    async getUserCoupons() {
	  let pageNum;
	  let pageSize;
	  if(this.current == 0){
		  pageNum = this.pageNum0
		  pageSize = this.pageSize0
	  }else if(this.current == 1){
		  pageNum = this.pageNum1
		  pageSize = this.pageSize1
	  }else if(this.current == 2){
		  pageNum = this.pageNum2
		  pageSize = this.pageSize2
	  }
      let params = {
        userId: this.userDetail.userId,
        status: this.current,
		pageNum: pageNum,
		pageSize: pageSize
      }
      let token = await this.getToken(params);
      let tmpToken = this.sault_md5_token(token);
      params.systemid = "payment";
      params.token = token;
      params.authStr = tmpToken;
      params.url = "/coupon/user/list";
      let result = await this.$u.api.unifyMiniRest(params);
      if (result.code == 200) {
		  if(this.current == 0){
			  if (this.pageNum0 != 1) {
				this.couponList0 = this.couponList0.concat(result.rows)
			  } else {
				this.couponList0 = result.rows
			  }
		  }else if(this.current == 1){
			  if (this.pageNum1 != 1) {
				this.couponList1 = this.couponList1.concat(result.rows)
			  } else {
				this.couponList1 = result.rows
			  }
		  }else if(this.current == 2){
			  if (this.pageNum2 != 1) {
				this.couponList2 = this.couponList2.concat(result.rows)
			  } else {
				this.couponList2 = result.rows
			  }
		  }
      }
    },
    async getUserCouponsCount(){
      let params = {
        userId: this.userDetail.userId,
      }
      let token = await this.getToken(params);
      let tmpToken = this.sault_md5_token(token);
      params.systemid = "payment";
      params.token = token;
      params.authStr = tmpToken;
      params.url = "/coupon/user/count";
      let result = await this.$u.api.unifyMiniRest(params);
      if (result.code == 200) {
		  this.NotUsedNum = result[0]
		  this.UsedNum = result[1]
		  this.ExpiredNum = result[2]
      }
      console.log(this.tabs)
    },
    async getToken(params) {
      params.systemid = "payment";
      params.url = "/points/token";
      let result = await this.$u.api.unifyMiniRest(params);
      let token = "";
      if (result.code == 200) {
        token = result.data
      }
      return token;
    },

    change(index) {
		this.current = index
		this.getUserCoupons()
    },

	/**
	 * 立即使用优惠券
	 */
	toUseCoupon(item) {
		if(item.useScope=='ALL'){
			uni.reLaunch({url: '/pages/index/evaluationIndex'})
		}else{
			this.$u.route('/pages_private_scene2/GroupPurchase/DistributionGoods?goodsId=' + item.useScope)
		}
	},

	/**
	 * 格式化优惠券金额
	 * @param {Object} couponAmount
	 */
	formatAmount(couponAmount){
	  if (couponAmount === void 0 || couponAmount === null){
	    return '';
    }
		let prefixAmount = couponAmount.split(".")[0]
		let suffixAmount = couponAmount.split(".")[1]
		if(suffixAmount == '00'){
			return prefixAmount
		}else{
			return couponAmount
		}
	},

	/**
	 * 格式化优惠券金额
	 * @param {Object} item
	 */
	formatDesc(item){
		let desc = ''
		if(item.useScope=='ALL'){
			desc = '全部商品可用'
		}else{
			if(item.couponName?.length > 10){
				desc = item.couponName.substring(0,10) + '...可用'
			}else{
				desc = this.couponName + '可用'
			}
		}
		return desc
	},

  }
}
</script>

<style scoped>
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

a {
  text-decoration: none;
  color: #000;
}

a, label, button, input, select {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

img {
  border: 0;
}

body {
  background: #fff;
  color: #666;
}

html, body, div, dl, dt, dd, ol, ul, li, h1, h2, h3, h4, h5, h6, p, blockquote, pre, button, fieldset, form, input, legend, textarea, th, td {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color: #08acee;
}

button {
  outline: 0;
}

img {
  border: 0;
}

button,input,optgroup,select,textarea {
  margin: 0;
  font: inherit;
  color: inherit;
  outline: none;
}

li {
  list-style: none;
}

a {
  color: #666;
}

.clearfix::after {
  clear: both;
  content: ".";
  display: block;
  height: 0;
  visibility: hidden;
}

.clearfix {
}

.divHeight {
  width: 100%;
  height: 10px;
  background: #f5f5f5;
  position: relative;
  overflow: hidden;
}

.r-line {
  position: relative;
}

.r-line:after {
  content: '';
  position: absolute;
  z-index: 0;
  top: 0;
  right: 0;
  height: 100%;
  border-right: 2rpx solid #D9D9D9;
  -webkit-transform: scaleX(0.5);
  transform: scaleX(0.5);
  -webkit-transform-origin: 100% 0;
  transform-origin: 100% 0;
}

.b-line {
  position: relative;
}

.b-line:after {
  content: '';
  position: absolute;
  z-index: 2;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2rpx;
  border-bottom: 2rpx solid #e2e2e2;
  -webkit-transform: scaleY(0.5);
  transform: scaleY(0.5);
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
}

.aui-flex {
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  align-items: center;
  padding: 30rpx;
  position: relative;
}

.aui-flex-box {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  flex: 1;
  min-width: 0;
  font-size: 24rpx;
  color: #333;
  /* padding: 12rpx 0; */
}

/* 必要布局样式css */
.aui-flexView {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
}

.aui-scrollView {
  width: 100%;
  height: 100%;
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  position: relative;
  padding-top: 70rpx;
}

.aui-navBar {
  height: 88rpx;
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  z-index: 1002;
  background: #fff;
}

.aui-navBar-item {
  height: 88rpx;
  min-width: 25%;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 25%;
  -ms-flex: 0 0 25%;
  flex: 0 0 25%;
  padding: 0 0.9rem;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  font-size: 0.7rem;
  white-space: nowrap;
  overflow: hidden;
  color: #353535;
  position: relative;
}

.aui-navBar-item:first-child {
  -webkit-box-ordinal-group: 2;
  -webkit-order: 1;
  -ms-flex-order: 1;
  order: 1;
  margin-right: -25%;
  font-size: 0.9rem;
  font-weight: bold;
}

.aui-navBar-item:last-child {
  -webkit-box-ordinal-group: 4;
  -webkit-order: 3;
  -ms-flex-order: 3;
  order: 3;
  -webkit-box-pack: end;
  -webkit-justify-content: flex-end;
  -ms-flex-pack: end;
  justify-content: flex-end;
}

.aui-center {
  -webkit-box-ordinal-group: 3;
  -webkit-order: 2;
  -ms-flex-order: 2;
  order: 2;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  height: 88rpx;
  width: 50%;
  margin-left: 25%;
}

.aui-center-title {
  text-align: center;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  display: block;
  text-overflow: ellipsis;
  font-size: 0.95rem;
  color: #333;
}

.icon {
  width: 40rpx;
  height: 40rpx;
  display: block;
  border: none;
  float: left;
  background-size: 40rpx;
  background-repeat: no-repeat;
}

.icon-return {
  background-image: url("");
}

.icon-more {
  background-image: url("https://dev-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/icon/Common/icon-more.png");
}

.tab-nav {
  z-index: 1002;
  right: 0;
  left: 0;
  top: 0;
  height: 80rpx;
  line-height: 80rpx;
  display: block;
  position: fixed;
  background: #fff;
  z-index: 1;
  width: 100%;
  margin: 0 auto;
  box-shadow: 0 0rpx 30rpx #ddd;
}

.tab-nav-item {
  height: 80rpx;
  position: relative;
  text-align: center;
  color: #585858;
  font-size: 26rpx;
  display: block;
  float: left;
  width: 33%;
}

.tab-nav-item.tab-active{
  color: #eb5468;
  font-weight: normal;
}

.tab-nav-item a {
  display: inherit;
  color: inherit;
  font-size: 28rpx;
  color: #474245;
}

.tab-panel {
  position: relative;
  overflow: hidden;
  margin: 4rpx 0;
}

.tab-panel .tab-panel-item {
  width: 100%;
  position: absolute;
  top: 0;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
}

.tab-nav-item.tab-active:before {
  content: '';
  width: 60%;
  height: 0px;
  position: absolute;
  left: 50%;
  bottom: 0;
  margin-left: -30%;
  z-index: 4;
  background-color: #eb5468;
  border: 2rpx solid #eb5468;
}

.tab-panel .tab-panel-item.tab-active ~ .tab-panel-item {
  -webkit-transform: translateX(100%);
  transform: translateX(100%);
}

.tab-panel .tab-panel-item.tab-active {
  position: relative;
  -webkit-transition: -webkit-transform .15s;
  transition: -webkit-transform .15s;
  transition: transform .15s;
  transition: transform .15s, -webkit-transform .15s;
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

.aui-show-box {

}

.aui-left-change {
  background-image: -webkit-gradient(linear,left top,right top,from(#529be8),to(#6175e0));
  background-image: -webkit-linear-gradient(left,#529be8,#6175e0);
  background-image: -moz-linear-gradient(left,#529be8,#6175e0);
  background-image: linear-gradient(to right,#529be8,#6175e0);
  background-color: #6175e0;
  text-align: center;
  color: #fff;
  padding: 24rpx 0rpx;
  height: 200rpx;
  width: 38%;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.aui-left-change h2 {
  font-size: 60rpx;
  display: flex;
}

.aui-left-change h2 em {
  font-style: normal;
  font-weight: normal;
  font-size: 40rpx;
}

.aui-flex-box h3 {
  font-size: 24rpx;
  width: auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-wrap: normal;
  word-wrap: break-word;
  word-break: break-all;
  padding-bottom: 20rpx;
  display: flex;
}

.aui-flex-box h3 em {
  background: #5293d5;
  font-size: 22rpx;
  font-style: normal;
  color: #fff;
  border-radius: 48rpx;
  padding: 5rpx 20rpx;
}

.aui-flex-box-two .aui-flex {
  padding: 0;
  /* line-height: 1.2; */
  /* overflow: hidden; */
  /* text-overflow: ellipsis; */
  /* display: -webkit-box; */
  /* -webkit-box-orient: vertical; */
  /* -webkit-line-clamp: 1; */
}

.aui-flex-bor-bom {
  border-bottom: 2rpx dashed #ddd;
  margin-bottom: 6rpx;
  /* padding-bottom: 19px; */
}

.aui-flex-box-two {
  overflow: hidden;
  position: relative;
  box-shadow: 2rpx 6rpx 26rpx #e4e4e4;
  height: 200rpx;
  padding: 20rpx;
  border-bottom: 2rpx solid #ececec;
  border-top: 2rpx solid #ececec;
  border-right: 2rpx solid #ececec;
}

.aui-icon-circle {
  background-image: url("https://dev-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/icon/Common/icon-circle.png");
  background-size: 20rpx;
  width: 40rpx;
  height: 340rpx;
  position: absolute;
  left: -10rpx;
  top: 0;
  background-repeat: repeat-y;
}

.aui-left-change p {
  font-size: 24rpx;
}

.aui-btn-use {
  background: none;
  border: 2rpx solid #eb5468;
  color: #eb5468;
  font-size: 0.7rem;
  border-radius: 40rpx;
  padding: 2rpx 10rpx;
  margin-bottom: 10rpx;
}

.aui-flex-box-flow {
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  padding-bottom: 0;
}

.tab-item {
  padding-bottom: 20rpx;
}

.tab-item .aui-flex {
  padding-bottom: 0;
}

.tab-item-list-green .aui-left-change {
  background-image: -webkit-gradient(linear,left top,right top,from(#54dedf),to(#30d0a6));
  background-image: -webkit-linear-gradient(left,#54dedf,#30d0a6);
  background-image: -moz-linear-gradient(left,#54dedf,#30d0a6);
  background-image: linear-gradient(to right,#54dedf,#30d0a6);
  background-color: #30d0a6;
}

.tab-item-list-green .aui-flex-box h3 em {
  background: #30d0a6;
}

.tab-item-list-green .aui-btn-use {
  border-color: #30d0a6;
  color: #30d0a6;
}

.fa-angle-right {
  background-image: url('https://dev-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/icon/Common/drop-down-icon.png');
}

.tab-item-list-default .aui-left-change {
  background: #c3c3c3;
}

.tab-item-list-default .aui-flex-box h3 em {
  background: #c3c3c3;
}

.aui-icon-used {
  width: 80rpx;
  height: 80rpx;
  position: absolute;
  top: 12rpx;
  right: -20rpx;
}

.aui-icon-used img {
  width: 100%;
  height: 80rpx;
  display: block;
  border: none;
}

.font-baseline {
  display: flex;
  align-items: baseline;
}

.tab-item-list-pink .aui-left-change {
  background-image: -webkit-gradient(linear,left top,right top,from(#eb5468),to(#eb5468));
  background-image: -webkit-linear-gradient(left,#eb5468,#eb5468);
  background-image: -moz-linear-gradient(left,#eb5468,#eb5468);
  background-image: linear-gradient(to right,#eb5468,#eb5468);
  background-color: #eb5468;
}

..tab-item-list-pink .aui-flex-box h3 em {
  background: #eb5468;
}

..tab-item-list-pink .aui-btn-use {
  border-color: #eb5468;
  color: #eb5468;
}

.aui-flex-show {
	padding-top: 8rpx !important;
	color: #bbbbbb;
}

</style>
